home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 Super Collection / Windows 95 Super Collection.iso / win95 / network / netuser / netuser.bas < prev    next >
Encoding:
BASIC Source File  |  1995-06-16  |  5.9 KB  |  202 lines

  1. Option Explicit
  2. '
  3. '   This module will return the user name of the person who signed into
  4. '   the system. This module should work with the following operating
  5. '   systems: Windows 3.x, Windows for Workgroups, Windows 95 and
  6. '   Windows NT.
  7. '
  8. '   This module is written for 16 bit applications, as 32 bit development
  9. '   environments become available this module will need to change.
  10. '
  11. '   This module requires the CALL32.DLL file to function correctly
  12. '   under Windows 95 and Windows NT. This DLL should be included with
  13. '   your application and copied to the users SYSTEM directory under
  14. '   windows.
  15. ''''
  16. '
  17. '   Declare variables needed
  18. '
  19. Dim mlngReturnStatus As Long
  20. Dim mintInitialized As Integer
  21. Dim mlngGetUserNameA As Long
  22. '
  23. '   Declare constants for windows version(s)
  24. '
  25. Const WV_WIN3X = 0
  26. Const WV_WINWFW = 1
  27. Const WV_WINNT = 2
  28. Const WV_WIN95 = 3
  29. '
  30. '   Constants used by API(s)
  31. '
  32. Const WF_WINNT = &H4000
  33.  
  34. Const WNNC_NET_MultiNet = &H8000
  35. Const WNNC_SUBNET_WinWorkgroups = 4
  36. Const WNNC_NET_TYPE = 2
  37.  
  38. '/* General */
  39.  
  40. Const WN_SUCCESS = &H0
  41. '
  42. '   API Declaration
  43. '
  44. Declare Function KRN_GetVersion Lib "kernel" Alias "GetVersion" () As Long
  45. Declare Function KRN_GetWinFlags Lib "Kernel" Alias "GetWinFlags" () As Long
  46. Declare Function USR_WNetGetCaps Lib "User" Alias "WNetGetCaps" (ByVal nIndex As Integer) As Integer
  47. Declare Function WFW_MNetNetworkEnum Lib "WFWNET.DRV" Alias "MNetNetworkEnum" (nIndex As Integer) As Integer
  48. Declare Function WFW_MNetSetNextTarget Lib "WFWNET.DRV" Alias "MNetSetNextTarget" (ByVal nIndex As Integer) As Integer
  49. Declare Function USR_WNetGetUser Lib "User" Alias "WNetGetUser" (ByVal sUser As String, nBufferSize As Integer) As Integer
  50. Declare Function Declare32& Lib "call32.dll" (ByVal func$, ByVal library$, ByVal args$)
  51. Declare Function GetUserNameA Lib "call32.dll" Alias "call32" (ByVal strUser As String, lngUserBuffer As Long, ByVal lngID As Long) As Integer
  52.  
  53. Private Sub InitializeWin32 ()
  54. '
  55. '   This routine will initialize the Win32 call interface.
  56. '
  57.     If Not mintInitialized Then
  58.     mlngGetUserNameA = Declare32("GetUserNameA", "advapi32.dll", "pp")
  59.     mintInitialized = True
  60.     End If
  61. End Sub
  62.  
  63. Private Function IsWFW () As Integer
  64. '
  65. '   This routine will determine if Windows for Workgroups is running.
  66. '
  67.     Dim intNetwork As Integer
  68.     
  69.     IsWFW = False
  70.     
  71.     intNetwork = USR_WNetGetCaps(WNNC_NET_TYPE)
  72.     If (intNetwork And WNNC_NET_MultiNet) Then
  73.     IsWFW = ((intNetwork And &HFFFF) And WNNC_SUBNET_WinWorkgroups) <> 0
  74.     End If
  75. End Function
  76.  
  77. Private Function IsWinNT () As Integer
  78. '
  79. '   This routine will determine if the user is running
  80. '   under the Windows NT system.
  81. '
  82.     Dim lngReturnStatus As Long
  83.  
  84.     IsWinNT = False
  85.     lngReturnStatus = KRN_GetWinFlags()
  86.     If lngReturnStatus And WF_WINNT Then
  87.     IsWinNT = True
  88.     End If
  89. End Function
  90.  
  91. Function NetworkUserID () As String
  92.  
  93. '   This routine will get the name of the user signed onto the network.
  94. '   If no username is found it will return an UnknownUser string.
  95. '
  96.     Dim strUser As String
  97.     Dim lngBufferSize As Long
  98.     Dim intHandle As Integer
  99.     Dim intEnumerate As Integer
  100.     Dim intVersion As Integer
  101.  
  102.     On Error GoTo NetworkUserID_ER
  103. '
  104. '   Initialize the buffer space needed
  105. '
  106.     lngBufferSize = 255
  107.     strUser = Space$(lngBufferSize)
  108. '
  109. '   Get the users current windows version
  110. '
  111.     intVersion = WindowsVersion()
  112.     Select Case intVersion
  113.     Case WV_WIN3X
  114.     mlngReturnStatus = USR_WNetGetUser(strUser, CInt(lngBufferSize))
  115.     If (mlngReturnStatus = WN_SUCCESS) Then
  116.         strUser = Left$(strUser, InStr(strUser, Chr(0)) - 1)
  117.     End If
  118.     Case WV_WINWFW
  119.     intHandle = 0
  120.     intEnumerate = WN_SUCCESS
  121.     intEnumerate = WFW_MNetNetworkEnum(intHandle)
  122. '
  123. '   Scan through the networks until we get a name
  124. '
  125.     While (intEnumerate = WN_SUCCESS)
  126.         mlngReturnStatus = WFW_MNetSetNextTarget(intHandle)
  127.         mlngReturnStatus = USR_WNetGetUser(strUser, CInt(lngBufferSize))
  128.         If (mlngReturnStatus = WN_SUCCESS) Then
  129.         strUser = Left$(strUser, InStr(strUser, Chr(0)) - 1)
  130.         End If
  131.         intEnumerate = WFW_MNetNetworkEnum(intHandle)
  132.     Wend
  133.     Case WV_WINNT, WV_WIN95
  134. '
  135. '   Initialize and call the Win32 API function(s)
  136. '
  137.     If Not mintInitialized Then InitializeWin32
  138.     mlngReturnStatus = GetUserNameA(strUser, lngBufferSize, mlngGetUserNameA)
  139.     If mlngReturnStatus <> 1 Then
  140.         MsgBox "Problem during NetworkUserID, problem code is " & Error
  141.         strUser = "UnknownUser"
  142.         Exit Function
  143.     End If
  144.     strUser = Left$(strUser, InStr(strUser, Chr(0)) - 1)
  145.     Case Else
  146.     strUser = ""
  147.     End Select
  148.  
  149.     If Len(strUser) = 0 Then
  150.     NetworkUserID = "UnknownUser"
  151.     Else
  152.     NetworkUserID = strUser
  153.     End If
  154.     Exit Function
  155.  
  156. NetworkUserID_ER:
  157.     MsgBox "Problem during NetworkUserID, problem code is " & Error
  158.     strUser = "UnknownUser"
  159.     Exit Function
  160. End Function
  161.  
  162. Private Function WindowsVersion () As Integer
  163. '
  164. '   This routine will determine the DOS/Windows version(s).
  165. '   It will return the values back to the calling program.
  166. '
  167.     Dim lngReturnStatus As Long
  168.     Dim strLowByte As String
  169.     Dim strHighByte As String
  170.     Dim sglWindowsVersion As Single
  171. '
  172. '   Check for Windows NT
  173. '
  174.     If IsWinNT() Then
  175.     WindowsVersion = WV_WINNT
  176.     Else
  177. '
  178. '   Since Windows NT is not running, find the version of windows
  179. '
  180.     lngReturnStatus = KRN_GetVersion()
  181.     lngReturnStatus = lngReturnStatus And &HFFFF&
  182.     strLowByte = Trim$(CStr(lngReturnStatus And &HFF))
  183.     strHighByte = Trim$(CStr((lngReturnStatus And &HFF00) / 256))
  184.     sglWindowsVersion = CSng(strLowByte & "." & strHighByte)
  185.     
  186.     Select Case sglWindowsVersion
  187.     Case Is < 3.95                 ' User is not under Windows 95
  188. '
  189. '   Check to see if the user is running WFW 3.11
  190. '
  191.         If IsWFW() Then
  192.         WindowsVersion = WV_WINWFW
  193.         Else
  194.         WindowsVersion = WV_WIN3X
  195.         End If
  196.     Case Else
  197.         WindowsVersion = WV_WIN95
  198.     End Select
  199.     End If
  200. End Function
  201.  
  202.